layui.use(['laypage', 'layer', 'form'], function () {
    let $ = layui.jquery, layer = layui.layer,form = layui.form;
    let id = 0, name = "";
    // 触发事件
    let  active = {
        view: function () {

        },
        edit: function () {
            location.href = '/admin/' + module + '/edit?id=' + id;
        },
        delete: function () {
            layer.confirm('确认删除' + moduleName + '"' + name + '"？', function () {

                $.post('/admin/' + module + '/delete', {idList: id}, function (data) {
                    if (data.code === 0) {
                        layer.msg('删除成功');
                        setTimeout(function () {
                            location.reload();
                        }, 1000);
                    } else {
                        layer.msg('删除失败');
                    }
                });
            });
        }
    };

    $('#operate a').on('click', function () {
        let othis = $(this), method = othis.data('method');
        if (method === 'edit' || method === 'delete') {
            id = othis.data('id');
            name = othis.data('name');
        }
        active[method] ? active[method].call(this, othis) : '';
    });

    // 监听提交
    form.on('submit(save)', function (data) {
        data = data.field;
        let opt = data.id !== '' ? '编辑' : '添加';
        $.ajax({
            type: 'post',
            url: '/admin/' + module + '/save',
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            success: function (result) {
                if (result.code === 0) {
                    layer.msg(opt + '成功。');
                    setTimeout(function () {
                        location.href="/admin/" + module;
                    }, 1000);
                } else {
                    layer.msg(result.msg);
                }
            }
        });
        return false;
    });
});